package com.omg.business.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.omg.business.BusinessInfoWrapper;
import com.omg.business.model.BusinessEvalInfo;
import com.omg.business.model.BusinessInfo;
import com.omg.business.model.SearchCriteria;
import com.omg.server.framework.dao.FacadeDAO;
import com.omg.server.util.CommonUtil;
import com.omg.util.ApplicationContextUtil;

public class GoodViewService {

	/**
	 * 获取商品列表信息
	 * @param pageIndex
	 * @return
	 */
	public BusinessInfoWrapper getBizInfoList(int pageIndex, int pageSize){
		FacadeDAO facadeDao = (FacadeDAO)ApplicationContextUtil.getBean("facadeDAO");
		
		Map<String, String> params = new HashMap<String, String>();
		List<Integer> countList = facadeDao.find_iBatis("omg.view.queryBizInfoCount", params);
		int count = 0;
		if (CommonUtil.isNotEmpty(countList)){
			count = countList.get(0).intValue();
		}
		
		if (pageIndex <= 0){
			pageIndex = 1;
		}
		if (pageSize <= 0){
			pageSize = 20;
		}
		if (pageSize > 30){
			pageSize = 20;
		}
		
		BusinessInfoWrapper wrapper = new BusinessInfoWrapper();
		wrapper.setPageIndex(pageIndex);
		wrapper.setTotal(count);
		List<BusinessInfo> retList = null;
		
		int offset = (pageIndex - 1) * pageSize;
		if (count > 0){
		   retList = facadeDao.find_iBatis("omg.view.queryBizInfoList", params, offset, pageSize);
		   wrapper.setList(retList);
		   wrapper.setPageSize(pageSize);
		}
		return wrapper;
	}
	
	/**
	 * 获取商品的评论信息
	 * @param pageIndex
	 * @return
	 */
	public BusinessInfoWrapper getBizEvalInfoList(int pageIndex, String businessId){
		FacadeDAO facadeDao = (FacadeDAO)ApplicationContextUtil.getBean("facadeDAO");
		
		List<Integer> countList = facadeDao.find_iBatis("omg.view.queryBizEvalCount", businessId);
		int count = 0;
		if (CommonUtil.isNotEmpty(countList)){
			count = countList.get(0).intValue();
		}
		
		if (pageIndex <= 0){
			pageIndex = 1;
		}
		int pageSize = 5;
		if (pageSize <= 0){
			pageSize = 5;
		}
		
		BusinessInfoWrapper wrapper = new BusinessInfoWrapper();
		wrapper.setPageIndex(pageIndex);
		wrapper.setTotal(count);
		List<BusinessEvalInfo> retList = null;
		
		int offset = (pageIndex - 1) * pageSize;
		if (count > 0){
		   retList = facadeDao.find_iBatis("omg.view.queryBizEvalInfo", businessId, offset, pageSize);
		   wrapper.setEvalList(retList);
		}
		return wrapper;
	}
	
	/**
	 * 保存商品评论信息
	 * @param pageIndex
	 * @return
	 */
	public String saveEvalInfo(String businessId, String commentInfo, String level, String author){
		String retVal = "";
		if (CommonUtil.isNotEmpty(businessId) && CommonUtil.isNotEmpty(commentInfo)){
			FacadeDAO facadeDao = (FacadeDAO)ApplicationContextUtil.getBean("facadeDAO");
			BusinessEvalInfo evalInfo = new BusinessEvalInfo();
			evalInfo.setId(UUID.randomUUID().toString());
			evalInfo.setBusinessId(businessId);
			evalInfo.setLevel(level);
			if (CommonUtil.isEmpty(author)){
			   evalInfo.setAuthor("游客");
			}
			else{
				evalInfo.setAuthor(author);
			}
			evalInfo.setCreateTime(new Date());
			evalInfo.setComments(commentInfo);
			int result = facadeDao.save_Hibernate_Object(evalInfo);
			retVal = "评论成功";
		}
		return retVal;
	}
	/**
	 * 搜索
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public BusinessInfoWrapper search(int pageIndex, int pageSize, SearchCriteria searchCriteria){
		FacadeDAO facadeDao = (FacadeDAO)ApplicationContextUtil.getBean("facadeDAO");
		
		Map<String, Object> params = new HashMap<String, Object>();
		String bizContent = searchCriteria.getBizContent();
		params.put("bizContent", bizContent);
		params.put("classify", searchCriteria.getClassify());
		if (searchCriteria.getBizSeq() != 0){
			params.put("bizSeq", searchCriteria.getBizSeq());
		}
		List<Integer> countList = facadeDao.find_iBatis("omg.view.queryBizInfoCount", params);
		int count = 0;
		if (CommonUtil.isNotEmpty(countList)){
			count = countList.get(0).intValue();
		}
		if (pageIndex <= 0){
			pageIndex = 1;
		}
		
		if (pageSize <= 0){
			pageSize = 20;
		}
		if (pageSize > 30){
			pageSize = 20;
		}
		
		BusinessInfoWrapper wrapper = new BusinessInfoWrapper();
		wrapper.setPageIndex(pageIndex);
		wrapper.setTotal(count);
		List<BusinessInfo> retList = null;
		
		int offset = (pageIndex - 1) * pageSize;
		if (count > 0){
		   retList = facadeDao.find_iBatis("omg.view.queryBizInfoList", params, offset, pageSize);
		   wrapper.setList(retList);
		   wrapper.setPageSize(pageSize);
		}
		return wrapper;
	}
	
	/**
	 * 根据序号查询目标链接地址
	 * @param bizSeq
	 * @return
	 */
	public String redirect(int bizSeq){
		FacadeDAO facadeDao = (FacadeDAO)ApplicationContextUtil.getBean("facadeDAO");
		List<String> targetUrl = facadeDao.find_iBatis("omg.view.queryTargetUrlBySeq", bizSeq);
		String finalUrl = "";
		if (CommonUtil.isNotEmpty(targetUrl)){
			finalUrl = targetUrl.get(0);
		}
		return finalUrl;
	}
}
